package com.ssplayer.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.ssplayer.application.MyApplication;
import com.ssplayer.bean.MusicAction;
import com.ssplayer.bean.MusicMsg;

public class VideoPlayerActivity extends Activity implements OnClickListener,
		OnTouchListener {

	private VideoView videoView;
	private LinearLayout button_back;
	private RelativeLayout title_layout;
	private TextView video_title;
	private Timer timer;
	private MediaController mediaController;
	private MyApplication myApp;
	private int currentTime = 0;

	private Handler handler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0x123:
				title_layout.setVisibility(View.GONE);
				mediaController.hide();
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		myApp = (MyApplication) getApplication();
		setContentView(R.layout.activity_video_player);
		findAllViews();
		playVideo();

	}

	private void findAllViews() {
		videoView = (VideoView) findViewById(R.id.videoView);
		button_back = (LinearLayout) findViewById(R.id.button_back);
		title_layout = (RelativeLayout) findViewById(R.id.title_layout);
		video_title = (TextView) findViewById(R.id.video_title);
		videoView.setOnTouchListener(this);
		button_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_back:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int action = event.getAction();
		switch (v.getId()) {
		case R.id.videoView:
			if (action == MotionEvent.ACTION_DOWN) {
				int visibilty = title_layout.getVisibility();
				if (visibilty == View.GONE) {
					showHideController();
				} else if (visibilty == View.VISIBLE) {
					title_layout.setVisibility(View.GONE);
					timer.cancel();
				}
			}
			return false;
		}
		return false;
	}

	private void showHideController() {
		title_layout.setVisibility(View.VISIBLE);
		timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				handler.sendEmptyMessage(0x123);
			}
		}, 3000);

	}

	@Override
	protected void onResume() {
		showHideController();
		currentTime = myApp.getMvideo_playtime();
		videoView.seekTo(currentTime);
		videoView.start();
		super.onResume();
	}

	@Override
	protected void onPause() {
		myApp.setMvideo_playtime(videoView.getCurrentPosition());
		super.onPause();
	}

	private void playVideo() {
		Intent intent = getIntent();
		mediaController = new MediaController(this);
		videoView.setVideoPath(intent.getStringExtra("path"));
		video_title.setText(intent.getStringExtra("title"));
		videoView.setMediaController(mediaController);
		pauseMusic();
	}

	/**
	 * 如果有音乐在播放则暂停音乐
	 */
	private void pauseMusic() {
		if (myApp.getMplay_status() == MusicMsg.STATUS_PLAYING) {
			Intent intent = new Intent();
			intent.setAction(MusicAction.MUSIC_SERVICE);
			intent.putExtra("msg", MusicMsg.PAUSE_MSG);
			startService(intent);
		}
	}

}
